package com.inossem.utils.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.inossem.utils.io.IOUtils;

import java.util.List;
import java.util.Map;

public class DatabaseUtils {

    public static final int CREATE_ERROR = 1;

    public static final int UPGRADE_ERROR = 2;
    
    public static final int FIRST_DATABASE_VERSION = 1;

    private static final String DATABASE_SP = "Utils_Database";

    private static final String DATABASE_FILENAME = "Database_FileName";// 数据库文件名称

    private static final String DATABASE_VERSION = "Database_Version";// 数据库版本

    private static final String DEFAULT_DATABASE_FILENAME="database.db";

    private static final int DEFAULT_DATABASE_VERSION = 1;

    /**
     * 初始化数据库
     * @author leij
     * @param context 上下文
     * @param databaseFileName 数据库文件名称
     * @param databaseVersion 数据库版本
     * @param createSql 数据库建表语句数组
     * @param upgradeSql 数据库更新表语句数组
     * @param databaseResponse 数据库响应监听
     */
    public static synchronized void initDatabase(Context context, String databaseFileName,
            int databaseVersion, List<String> createSql, Map<Integer, List<String>> upgradeSql,
            DatabaseResponse databaseResponse) {
        // 初始化数据库时设置数据库名称
        saveDatabaseFileName(context, databaseFileName);
        // 初始化数据库时设置版本号
        saveDatabaseVersion(context, databaseVersion);
        DatabaseHelper databaseHelper = new DatabaseHelper(context, databaseFileName,
                databaseVersion, createSql, upgradeSql, databaseResponse);
        // 调用下边方法时才会执行onCreate方法和onUpgrade方法
        SQLiteDatabase sqliteDatabase = databaseHelper.getReadableDatabase();
        closeAll(sqliteDatabase, databaseHelper);
    }

    /**
     * 获取DatabaseHelper对象
     * @author leij
     * @param context 上下文
     * @return DatabaseHelper对象
     */
    public static synchronized DatabaseHelper getDatabaseHelper(Context context) {
        // 获取当前数据库名称
        String databaseFileName = getDatabaseFileName(context);
        // 获取当前数据库版本号
        int databaseVersion = getDatabaseVersion(context);
        return new DatabaseHelper(context, databaseFileName, databaseVersion, null, null, null);
    }

    /**
     * 释放资源
     * @author leij
     * @param cursor cursor对象
     * @param sqliteDatabase sqliteDatabase对象
     * @param databaseHelper databaseHelper对象
     */
    public static void closeAll(Cursor cursor, SQLiteDatabase sqliteDatabase,
            DatabaseHelper databaseHelper) {
        if (cursor != null) {
            cursor.close();
        }
        if (sqliteDatabase != null) {
            sqliteDatabase.close();
        }
        if (databaseHelper != null) {
            databaseHelper.close();
        }
    }

    /**
     * 释放资源
     * @author leij
     * @param sqliteDatabase sqliteDatabase对象
     * @param databaseHelper databaseHelper对象
     */
    public static void closeAll(SQLiteDatabase sqliteDatabase, DatabaseHelper databaseHelper) {
        closeAll(null, sqliteDatabase, databaseHelper);
    }

    /**
     * 释放资源
     * @author leij
     * @param databaseHelper databaseHelper对象
     */
    public static void closeAll(DatabaseHelper databaseHelper) {
        closeAll(null, null, databaseHelper);
    }

    private static void saveDatabaseFileName(Context context,String databaseFileName){
        IOUtils.putString(context, DATABASE_SP, DATABASE_FILENAME, databaseFileName);
    }

    private static void saveDatabaseVersion(Context context,int databaseVersion){
        IOUtils.putInt(context, DATABASE_SP, DATABASE_VERSION, databaseVersion);
    }

    private static String getDatabaseFileName(Context context){
        return IOUtils.getString(context, DATABASE_SP, DATABASE_FILENAME, DEFAULT_DATABASE_FILENAME);
    }

    private static int getDatabaseVersion(Context context){
        return IOUtils.getInt(context, DATABASE_SP, DATABASE_VERSION, DEFAULT_DATABASE_VERSION);
    }

    public interface DatabaseResponse {

        public void onFailure(int errorCode);

        public void onSuccess();
    }
}
